//
//  BattleDetailsViewController.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/14.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "BattleDetailsViewController.h"
#import "LiveHandle.h"
#import "LiveTableViewCell.h"
#import "LiveModel.h"
#import "WebViewController.h"
#import <MJRefresh.h>
@interface BattleDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,LiveTableViewCellDelegate>

@property(nonatomic,strong)UITableView *tableView;
@end

@implementation BattleDetailsViewController



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
  [self upData];
}







- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-44*LAYOUT_SIZE_HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView registerClass:[LiveTableViewCell class] forCellReuseIdentifier:@"LiveTableViewCell"];
    [self replaceNAvigationBar];

   self.tableView.tableFooterView = [[UIView alloc]init];

}
#pragma mark - 替换导航栏
-(void)replaceNAvigationBar
{
    self.navigationController.navigationBar.hidden = YES;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, 64*LAYOUT_SIZE_HEIGHT)];
    view.backgroundColor =ColorHex(0x0d1c38);
    [self.view addSubview:view];
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - (25 * LAYOUT_SIZE), 29 * LAYOUT_SIZE_HEIGHT, 50* LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    label.text = @"赛程";
    label.textColor = [UIColor yellowColor];
    label.textAlignment = 1;
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17*LAYOUT_SIZE];
    
    [view addSubview:label];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10 * LAYOUT_SIZE, 29 * LAYOUT_SIZE_HEIGHT, 50 * LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    
    [button setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    
}
#pragma mark - 返回按钮的事件
-(void)leftButtonAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
   
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return [[LiveHandle sharedLiveHandle].battleDetailsArray count];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveTableViewCell"];
    if ([LiveHandle sharedLiveHandle].battleDetailsArray) {
        LiveModel *model = [LiveHandle sharedLiveHandle].battleDetailsArray[indexPath.row];
        [cell setCellDataFromLiveModel:model];

    }
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
  
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190*LAYOUT_SIZE_HEIGHT;
}

#pragma mark - 代理方法
-(void)LiveTableViewCellCountBtnDidClicked:(LiveTableViewCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    LiveModel *liveModel = [LiveHandle sharedLiveHandle].battleDetailsArray[indexPath.row];
    NSString *urlString = liveModel.link2url;
    [self jumpToWebViewWith:urlString];
    
    
}


-(void)LiveTableViewCellVideoOrTextBtnDidClicked:(LiveTableViewCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    LiveModel *liveModel = [LiveHandle sharedLiveHandle].battleDetailsArray[indexPath.row];
    NSString *urlString = liveModel.m_link1url;
    [self jumpToWebViewWith:urlString];
    
    
}
#pragma mark - 跳转到webView
-(void)jumpToWebViewWith:(NSString *)str
{
    NSURL *url = [NSURL URLWithString:str];
    WebViewController *webViewVC =[[WebViewController alloc]init];
    webViewVC.url = url;

    [self.navigationController pushViewController:webViewVC animated:YES];
    
}
#pragma mark - 下拉刷新
-(void)upData
{

    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
    
}
#pragma mark  - 刷新执行的方法
-(void)refreshAction
{
    [LiveHandle sharedLiveHandle].liveTodayDataArray = nil;
    __weak typeof(self) weakSlef = self;
    [[LiveHandle sharedLiveHandle] getDataFromNetWorkWithBlock:^(BOOL isFinished) {
        if (isFinished) {
            [weakSlef.tableView reloadData];
        }else{
            
            [[LiveHandle sharedLiveHandle] getDataFromNetWorkWithBlock:^(BOOL isFinished) {
                
            }];
        }
        [weakSlef.tableView.mj_header endRefreshing];
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [LiveHandle sharedLiveHandle].battleDetailsArray = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
